package com.pirestupppai.ui.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.blog.www.guideview.Component;
import com.blog.www.guideview.Guide;
import com.blog.www.guideview.GuideBuilder;
import com.pirestupppai.R;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.common.Constant;
import com.pirestupppai.imagetool.virtualtool.VirtualView;
import com.pirestupppai.utils.GuideComponent;
import com.pirestupppai.utils.SharedPreferenceUtil;

import butterknife.BindView;

/**
 *  图片虚化页面
 */

public class ImageVirtualActivity extends BaseActivity {

    @BindView(R.id.virtual_view)
    VirtualView mImageVirtualView;
    @BindView(R.id.seekbar)
    SeekBar mAlphaSeekBar;
    @BindView(R.id.complete_btn)
    ImageView mCompleteBtn;
    @BindView(R.id.back_btn)
    ImageView mBackBtn;

    private int mCurrentAlpha;

    private Guide mGuideOperation;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected void initListeners() {
        mAlphaSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mCurrentAlpha = (int)(255 * (float)progress / (float)255);
                mImageVirtualView.setImageAlpha(mCurrentAlpha);
                mImageVirtualView.invalidate();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mCompleteBtn.setOnClickListener(this);
        mBackBtn.setOnClickListener(this);
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_image_virtual;
    }

    @Override
    protected void initLogic() {
        mImageVirtualView.setBitmap(ImageEditActivity.mDeliverBitmap);

        SharedPreferences sp = SharedPreferenceUtil.getSp();
        SharedPreferences.Editor editor = SharedPreferenceUtil.getEditor();

        boolean isFirstRun = sp.getBoolean(Constant.SPKey.FIRSTIMAGEVIRTUALACTIVITY, true);
        if (isFirstRun) {
            editor.putBoolean(Constant.SPKey.FIRSTIMAGEVIRTUALACTIVITY, false);
            editor.apply();

            // 引导层
            mAlphaSeekBar.post(new Runnable() {
                @Override
                public void run() {
                    ShowSeekBarGuideView();
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complete_btn:
                ImageEditActivity.mDeliverBitmap = mImageVirtualView.getmBackgroundBitmap();
                setResult(RESULT_OK);
                this.finish();
                break;
            case R.id.back_btn:
                this.finish();
                break;
        }
    }

    public void ShowSeekBarGuideView() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(mAlphaSeekBar)
                .setAlpha(150)
                .setHighTargetCorner(10)
                .setHighTargetPadding(10)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        builder.addComponent(new GuideComponent(mContext, "拖动以调整图层透明度", Component.ANCHOR_TOP, Component.FIT_START, 100, -50));
        mGuideOperation = builder.createGuide();
        mGuideOperation.setShouldCheckLocInWindow(false);
        mGuideOperation.show(ImageVirtualActivity.this);
    }
}
